package com.ruoyi.system.service.impl.video;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.model.VsUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.video.VsDramasVideo;
import com.ruoyi.system.domain.video.VsDramasVideoEpisodes;
import com.ruoyi.system.domain.video.VsDramasVideoFavorite;
import com.ruoyi.system.mapper.video.VsDramasVideoEpisodesMapper;
import com.ruoyi.system.mapper.video.VsDramasVideoFavoriteMapper;
import com.ruoyi.system.mapper.video.VsDramasVideoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.video.VsDramasVideoLogMapper;
import com.ruoyi.system.domain.video.VsDramasVideoLog;
import com.ruoyi.system.service.video.IVsDramasVideoLogService;

/**
 * 追剧记录Service业务层处理
 * 
 * @author zzx
 * @date 2024-04-30
 */
@Service
public class VsDramasVideoLogServiceImpl implements IVsDramasVideoLogService 
{
    @Autowired
    private VsDramasVideoLogMapper vsDramasVideoLogMapper;

    @Autowired
    private VsDramasVideoFavoriteMapper vsDramasVideoFavoriteMapper;

    @Autowired
    private VsDramasVideoMapper vsDramasVideoMapper;

    @Autowired
    private VsDramasVideoEpisodesMapper vsDramasVideoEpisodesMapper;

    /**
     * 查询追剧记录
     * 
     * @param id 追剧记录主键
     * @return 追剧记录
     */
    @Override
    public VsDramasVideoLog selectVsDramasVideoLogById(Integer id)
    {
        return vsDramasVideoLogMapper.selectVsDramasVideoLogById(id);
    }

    /**
     * 查询追剧记录列表
     * 
     * @param vsDramasVideoLog 追剧记录
     * @return 追剧记录
     */
    @Override
    public List<VsDramasVideoLog> selectVsDramasVideoLogList(VsDramasVideoLog vsDramasVideoLog)
    {
        VsUser vsUser = null;
        try {
            vsUser = SecurityUtils.getLoginUser().getVsUser();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        if (vsUser != null) {
            VsDramasVideoFavorite favorite = new VsDramasVideoFavorite();
            favorite.setType("favorite");
            favorite.setUserId(vsUser.getId());
            List<VsDramasVideoFavorite> favoriteList = vsDramasVideoFavoriteMapper.selectVsDramasVideoFavoriteList(favorite);
            vsDramasVideoLog.setType("log");
            vsDramasVideoLog.setUserId(vsUser.getId());
            // 查当前用户的追剧信息，肯定不是只查一部剧，所以把剧集id都置为空
            vsDramasVideoLog.setVid(null);
            vsDramasVideoLog.setEpisodeId(null);
            List<VsDramasVideoLog> list = vsDramasVideoLogMapper.selectVsDramasVideoLogList(vsDramasVideoLog);
            for (VsDramasVideoLog log : list) {
                VsDramasVideoEpisodes episodes = vsDramasVideoEpisodesMapper.selectVsDramasVideoEpisodesById(Math.toIntExact(log.getEpisodeId()));
                VsDramasVideo video = vsDramasVideoMapper.selectVsDramasVideoById(Math.toIntExact(log.getVid()));
                log.setVideo(video);
                log.getVideo().setCurrentEpisodeId(log.getEpisodeId());
                log.getVideo().setCurrentName(episodes.getName());
                log.getVideo().setCurrentViewTime(log.getViewTime());
                int num = 0;
                for (VsDramasVideoFavorite f : favoriteList) {
                    if (log.getVid().equals(f.getVid())) {
                        num++;
                    }
                }
                if (num > 0) {
                    log.getVideo().setFavorite(true);
                } else {
                    log.getVideo().setFavorite(false);
                }
            }
            return list;
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 新增追剧记录
     * 
     * @param vsDramasVideoLog 追剧记录
     * @return 结果
     */
    @Override
    public int insertVsDramasVideoLog(VsDramasVideoLog vsDramasVideoLog)
    {
        VsUser vsUser = SecurityUtils.getLoginUser().getVsUser();
        if (vsUser != null) {
            vsDramasVideoLog.setUserId(vsUser.getId());
        }

        VsDramasVideoLog log = new VsDramasVideoLog();
        log.setUserId(vsUser.getId());
        log.setVid(vsDramasVideoLog.getVid());
        List<VsDramasVideoLog> videoLogs = vsDramasVideoLogMapper.selectVsDramasVideoLogList(log);
        if (videoLogs.size() > 0) {
            vsDramasVideoLog.setId(videoLogs.get(0).getId());
            vsDramasVideoLog.setUpdatetime(System.currentTimeMillis());
            return vsDramasVideoLogMapper.updateVsDramasVideoLog(vsDramasVideoLog);
        }
        vsDramasVideoLog.setCreatetime(System.currentTimeMillis());
        vsDramasVideoLog.setUpdatetime(System.currentTimeMillis());
        return vsDramasVideoLogMapper.insertVsDramasVideoLog(vsDramasVideoLog);
    }

    /**
     * 修改追剧记录
     * 
     * @param vsDramasVideoLog 追剧记录
     * @return 结果
     */
    @Override
    public int updateVsDramasVideoLog(VsDramasVideoLog vsDramasVideoLog)
    {
        vsDramasVideoLog.setUpdatetime(System.currentTimeMillis());
        return vsDramasVideoLogMapper.updateVsDramasVideoLog(vsDramasVideoLog);
    }

    /**
     * 批量删除追剧记录
     * 
     * @param ids 需要删除的追剧记录主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasVideoLogByIds(Integer[] ids)
    {
        return vsDramasVideoLogMapper.deleteVsDramasVideoLogByIds(ids);
    }

    /**
     * 删除追剧记录信息
     * 
     * @param id 追剧记录主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasVideoLogById(Integer id)
    {
        return vsDramasVideoLogMapper.deleteVsDramasVideoLogById(id);
    }

    @Override
    public int removeLog(VsDramasVideoLog vsDramasVideoLog) {
        return vsDramasVideoLogMapper.removeLog(vsDramasVideoLog);
    }
}
